package edu.mju.scenic.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.mju.scenic.entity.Img;
import edu.mju.scenic.service.ImgService;
import edu.mju.scenic.vo.IndexSpotVo;
import edu.mju.serviceutils.ResponseCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author CBL
 * @since 2020-12-28
 */
@RestController
@RequestMapping("/scenic/img")
//@CrossOrigin
public class ImgController {

    @Autowired
    private ImgService imgService;

    @GetMapping("/getImgInfo/{page}/{size}")
    public ResponseCode getImgInfo(@PathVariable Long page,
                                   @PathVariable Long size){
        IPage<Img> imgIPage = new Page<>(page,size);
        Map<String,Object> map = imgService.getImgInfo(imgIPage);
        return ResponseCode.success().data(map).data("page",page);
    }

    /**
     * 更新图片信息
     */
    @PostMapping("/updateScenicSpotImg")
    public ResponseCode updateScenicSpot(@RequestBody Img img){
        boolean b = imgService.updateById(img);
        if(b){
            return ResponseCode.success();
        }
        return ResponseCode.error();
    }

    /**
     * 更新图片信息
     */
    @PostMapping("/addScenicSpotImg")
    public ResponseCode addScenicSpotImg(@RequestBody Img img){
        boolean b = imgService.save(img);
        if(b){
            return ResponseCode.success();
        }
        return ResponseCode.error();
    }

    @DeleteMapping("/deleteScenicImgById/{id}")
    public ResponseCode deleteScenicImgById(@PathVariable Long id){
        boolean b = imgService.deleteById(id);
        if(b){
            return ResponseCode.success();
        }
        return ResponseCode.error();
    }
}

